package com.legrand.www.module.authentication.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.legrand.www.module.authentication.dto.TokenDTO;
import com.legrand.www.module.authentication.mapper.UserMapper;
import com.legrand.www.module.common.util.EncryptionUtil;
import com.legrand.www.module.core.entity.User;
import com.legrand.www.module.authentication.service.UserService;
import com.legrand.www.module.core.redis.RedisUtils;
import com.legrand.www.module.core.redis.StoreTime;
import com.legrand.www.module.core.util.JwtToken;
import com.legrand.www.module.core.util.MD5Util;
import com.legrand.www.module.core.util.SHA1Util;
import com.legrand.www.module.core.util.SerializeUtil;
import com.legrand.www.module.exception.exception.CustomizeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * @author xiah
 * date: 2018/6/26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtils redisUtils;

       /*@Override
       public User getUser() {
              *//*使用mybatis-plus分页*//*
              EntityWrapper wrapper = new EntityWrapper();
              wrapper.where("id>{0}", 0);
              *//*从第一条记录开始   不包括第一条   最多返回三条记录*//*
              RowBounds rowBounds = new RowBounds(1, 3);
              List list = userMapperer.selectPage(rowBounds, wrapper);
               *//*使用自定义分页 当前页 每页多少条*//*
              Page page = new Page(1, 3);
              List list2 = userMapperer.getList(page);
              User user = userMapperer.selectById(1L);
              return user;
       }*/

    @Override
    public User getUser() {
        userMapper.getClass();
        return null;
    }

    /**
     * @author xiah
     * 2018-06-27 04:01:08
     * 验证账号和密码是否正确 密码正确返回对应的token
     */
    @Override
    public TokenDTO checkUser(String userName, String password) throws Exception {
        User user = new User();
        user.setUserName(userName);
        User u = this.userMapper.selectOne(user);
        /**用户密码正确*/
        if (u != null && u.getPassword().equals(EncryptionUtil.md5(password))) {
            final String accessToken = JwtToken.createToken();
            final String refreshToken = JwtToken.createPermanentToken();
            /**将accessToken和refreshToken加密后存储到redis*/
            Random random = new Random();
            String type = "MD5";
            if (random.nextInt(2) % 2 == 0) {
                String access = MD5Util.encode(accessToken);
                redisUtils.set(access, SerializeUtil.serialize(user), StoreTime.THREE_DAY);
                redisUtils.set(MD5Util.encode(refreshToken), access, StoreTime.THIRTY_DAY);
            } else {
                type = "SHA1";
                String access = SHA1Util.encode(accessToken);
                redisUtils.set(access, SerializeUtil.serialize(user), StoreTime.THREE_DAY);
                redisUtils.set(SHA1Util.encode(refreshToken), access, StoreTime.THIRTY_DAY);
                User ee = (User) SerializeUtil.unSerialize(redisUtils.get(access).toString());
                System.out.println(ee.getUserName());
            }

            return new TokenDTO(accessToken, refreshToken, type);
        }
        throw new CustomizeException("501", "userName or password is wrong!");
    }

    /**
     * 删除用户的token，一个用户只能有一组有效token
     */
    public void searchById(String id) {
        Object object = redisUtils.get(id);
        if (object != null) {
            List<String> list = (List) object;
            for (String key : list) {
                redisUtils.remove(key);
            }
        }
    }
}
